<?php
namespace app\api\controller;
use app\common\controller\BaseApi;

/*东夏软件  对接*/
class DXApi extends  BaseApi
{
	//进出门禁
	const GetContractInfo="/GetMemberInfo";// 获取会员信息
	const CheckIn="/CheckCardindex";//进场
	const ConfirmCheckOut="/CheckCardindex"; //出场
	
	protected $url;//东夏接口url
	protected $dxshopid;//东兴店铺id
	protected $loginid;//员工id
	protected $password;//登录密码
	
	protected $shop_id;//中未智联店铺id
	
	/**
	 * 测试地址
	 * http://SmartGYM.com/api/DXApi/GetPTEmployeeList/v/1/t/1/key/zwzl_admin/sign/5de093255ad3e78341dbe00309c9436c/shop_id/68
	 * */
	 public function _initialize()
    {
//  	parent::_initialize();
    }
	/////////////////////////////////////////获取安科数据////////////////////////////////////////////////////////////////
	/**
	 * 方法前操作获取店铺信息
	 */
	function before_method($shop_id){
		if(empty($shop_id)){
			$this->ajax_error('','',"店铺id 为空");
		}else{
			$this->shop_id=$shop_id;
		}
		$config=cache("ExtraConfig_".$this->shop_id);
    	if(empty($config)){
			 $config=db("Shop")->where("id=".$this->shop_id)->value("extra_config");
			 if(empty($config)){
			 	$this->ajax_error('','',"请配置东夏代码");
			}
			 $config=json_decode($config,TRUE);
			 cache("ExtraConfig_".$this->shop_id,$config);
    	}
		$this->url=$config['url'];
    	$this->dxshopid=$config['dxshopid'];
		$this->loginid=$config['loginid'];
		$this->password=$config['password'];
	}
	/**
	 * 获取合同号
	 * @param $card_no
	 * @return $data
	 * [
	 *	    {
	 *		        "ContractID": "06162ef0-7022-433e-80bd-7d9d44dc113c",
	 *		        "MemberID": "5c5313c8-aa5b-493a-8664-88e89b19a6ca",
	 *		        "MemberName": "ERCC",
	 *		        "Sex": "鐢�",
	 *		        "Mobile": "18523020102",
	 *		        "CardNo": "2238082314",
	 *		        "StartDate": "2017-10-19",
	 *		        "EndDate": "2018-10-28",
	 *		        "ContractStatus": 1,
	 *		        "MembershipType": 2,
	 *		        "MembershipTypeName": "骞存湀鍗�",
	 *		        "SurplusNum": "0",
	 *		        "StoredSumValue": "0.00"
	 *		    }
	 *		]
	 * http://smartgym.com/api/DXApi/GetContractInfo/card_no/0723087095/shop_id/12
	 */
	function GetContractInfo($card_no,$shop_id){
		$this->before_method($shop_id);
		$result=http_get($this->url.self::GetContractInfo,['cardindex'=>$card_no,'shopid'=>$this->dxshopid,"loginid"=>$this->loginid,"password"=>$this->password,"shopname"=>"","loginname"=>""]);
		if($result['flag']){
			$json= xml_json($result['data'],1);
			if(!isset($json['diffgr_diffgram']['NewDataSet'])||empty(isset($json['diffgr_diffgram']['NewDataSet']))){
				return template_error('配置错误或者同步的卡号无效(东夏)');
			}else{
				$json=$json['diffgr_diffgram']['NewDataSet']['Table'];
				return template_success("成功(东夏)",array(
														  "ContractID"=>$json['cardIndex']."_".time(),
														  "MemberID"=>$json['cardIndex']."_".time(),
														  "StartDate"=>substr($json['activeDate'], 0,10),
														   "MemberName"=>$json['name'],
														  "EndDate"=>substr($json['endDate'], 0,10),
														  "ContractStatus"=>1,
														  "Sex"=>"",
														  "Mobile"=>empty($json["phone"])?'':$json["phone"],
														  "CardNo"=>$card_no,
														  "MembershipType"=>1,
														  "MembershipTypeName"=>$json['cardName'],
														  "SurplusNum"=>0,
														  "StoredSumValue"=>0,
														  )
														  );
			}			
		}else{
			return template_error($result['msg']);
		}
	}
	/**
	 * 入场操作
	 * @param $card_no
	 * http://smartgym.com/api/DXApi/CheckIn/card_no/0723087095/shop_id/12
	 */
	function CheckIn($card_no,$shop_id){
		$this->before_method($shop_id);
		$result=http_get($this->url.self::CheckIn,['cardindex'=>$card_no,'shopid'=>$this->dxshopid,"loginid"=>$this->loginid,"password"=>$this->password,"mode"=>"only_in","brushnum"=>2,"repeat"=>"yes","billtype"=>"new","shopname"=>"","loginname"=>"","projectname"=>""]);
		if($result['flag']){
			$json= xml_json($result['data'],1);
			if(empty($json)){
				return template_error("配置错误或者卡号错误(东夏)");
			}else{
				return template_success("成功(东夏)",$json[0]);
			}			
		}else{
			return template_error($result['msg']);
		}
	}
	/**
	 * 出场操作
	 * @param $card_no
	 */
	function ConfirmCheckOut($card_no,$shop_id){
		$this->before_method($shop_id);
		$result=http_get($this->url.self::ConfirmCheckOut,['cardindex'=>$card_no,'shopid'=>$this->dxshopid,"loginid"=>$this->loginid,"password"=>$this->password,"mode"=>"only_out","brushnum"=>2,"repeat"=>"yes","billtype"=>"new","shopname"=>"","loginname"=>"","projectname"=>""]);
		if($result['flag']){
			$json= xml_json($result['data'],1);
			if(empty($json)){
				return template_error("配置错误或者卡号错误(东夏)");
			}else{
				return template_success("成功(东夏)",$json[0]);
			}			
		}else{
			return template_error($result['msg']);
		}
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
